package com.ybkj.o2o.sales.controller.web;

import com.ybkj.o2o.sales.common.DataForm;
import com.ybkj.o2o.sales.common.DataGrid;
import com.ybkj.o2o.sales.entity.Application;
import com.ybkj.o2o.sales.manager.ApplicationManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by shine on 15/8/21.
 */
@RequestMapping("applications")
@Controller
public class ApplicationController {

  @Autowired
  private ApplicationManager applicationManager;

  @RequestMapping(value = {"", "/"}, method = RequestMethod.GET)
  public String index() {

    return "application/list";
  }

  @RequestMapping(value = {"datagrid"}, method = RequestMethod.GET)
  public @ResponseBody DataGrid<Application> list(@RequestParam("page") int page,
      @RequestParam("rows") int rows) {

    return applicationManager.findAll(page, rows);
  }


  @RequestMapping(value = {"", "/"}, method = RequestMethod.POST)
  public @ResponseBody DataForm<Application> add(@ModelAttribute Application application) {

    return applicationManager.insert(application);
  }

  @RequestMapping(value = "{id}", method = RequestMethod.GET)
  public @ResponseBody Application find(@PathVariable("id") Long id) {

    return applicationManager.fetch(id);
  }

  @RequestMapping(value = "{id}/tokens", method = RequestMethod.PATCH)
  public @ResponseBody DataForm<Application> update(@PathVariable("id") Long id) {

    return applicationManager.updateAppkey(id);
  }

  @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
  public @ResponseBody DataForm<Application> delete(@PathVariable("id") Long id) {

    return applicationManager.deleteByPrimaryKey(id);
  }
}
